﻿package dungeonCode 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.display.MovieClip;
	import flash.media.Sound; 
	import flash.net.URLRequest; 
	
	/**
	 * @author Josh Davis
	 * Sound effect recorded by Mike Koenig
	 */
	
	public class Game extends Sprite 
	{
		private var tiles:Array;
		private var background:Array;
		private var levelXML:XML;
		private var hud:HUD;
		
		public var mouseIsDown:Boolean = false;
		private var collides:Boolean = false;
		private var gameOver:Boolean = false;
		
		private var currentLevel:int = -1;
		
		public function Game(file:XML) 
		{
			super();
			levelXML = file;
			hud = new HUD();
		}
		
		public function begin():void
		{
			addChild(hud);
			background = new Array();
			tiles = new Array();
			nextRoom();
		}
		
		public function tileDown(e:Event):void
		{
			if (!gameOver)
			{
				hud.takePoints(25);
				mouseIsDown = true;
			}
		}
		
		public function tileUp(e:Event):void
		{
			if (!gameOver)
			{
				mouseIsDown = false;
				for (var i:Number = 0; i < tiles.length; i++)
				{
					tiles[i].selected = false;
				}
			}
		}
		
		public function update(e:Event):void
		{
			//if not game over
			if (!gameOver)
			{
				if (mouseIsDown)
				{
					for (var a:Number = 0; a < tiles.length; a++)
					{
						// this tile is not colliding (yet):
						collides = false;

						if (tiles[a].selected)
						{
							// assume the last position is safe
							tiles[a].prev(tiles[a].x, tiles[a].y);

							// change and check the new position
							if (tiles[a].getType() == 0)
							{
								if(tiles[a].currentFrame > 9)
									Tile(tiles[a]).gotoAndStop("hero");
								tiles[a].x = mouseX;
								tiles[a].y = mouseY;
								tiles[a].play();
							}
							else if (tiles[a].getType() == 1)
								tiles[a].x = mouseX;
							else if (tiles[a].getType() == 2)
								tiles[a].y = mouseY;

							for (var b:Number = 0; b < tiles.length; b++)
							{
								if (tiles[a].hitTestObject(tiles[b]) && b != a)
								{
									// this tile is colliding
									if (tiles[b].getType() == 3)
									{
										hud.gotTreasure(500);
										tiles[b].destroy();
										tiles.splice(b, 1);
									}
									else
									{
										collides = true;
									}
								}
							}

							// I've collided
							if (collides)
							{
								tiles[a].revert();
								mouseIsDown = false;
							}
						}
					}
				}
				else if(tiles[0].currentFrame != 0)
				{
					Tile(tiles[0]).gotoAndStop("hero");
				}
				
				if (tiles[0].x > 575)
				{
					nextRoom();
				}
				else if(tiles[0].x < 50)
				{
					prevRoom();
				}
			}
			else
			{
				//prevent infinite clip playing glitch at the end
				if(tiles[0].currentFrame != 0)
				{
					Tile(tiles[0]).gotoAndStop("hero");
				}
			}
		}
		
		private function nextRoom():void 
		{
			currentLevel++;
			if (currentLevel == 0)
			{
				for (var i:Number = 0; i < levelXML.levels[0].bg[currentLevel].tile.length(); i++)
				{
					background[i] = new Tile();
					addChild(background[i]);
					background[i].init(levelXML.levels.bg[currentLevel].tile[i].@type, levelXML.levels.bg[currentLevel].tile[i].@x, levelXML.levels.bg[currentLevel].tile[i].@y);
				}
				hud.tutorial_txt.text = "Click and drag the hero to a door to enter the next room.";
			}
			else if (currentLevel == 1)
			{
				hud.tutorial_txt.text = "Collect treasures for valuable bonus points.";
			}
			else if (currentLevel == 2)
			{
				hud.tutorial_txt.text = "Slide walls by clicking and dragging.";
			}
			else
			{
				hud.tutorial_txt.text = "";
			}

			if(currentLevel < (levelXML.levels[0].level as XMLList).length())
			{        
				if (currentLevel != 0)
				{
					for (var j:Number = tiles.length - 1; j >= 0; j--)
					{
						tiles[j].destroy();
						tiles.splice(j, 1);
					}
				}
			
				for (var k:Number = 0; k < levelXML.levels[0].level[currentLevel].tile.length(); k++)
				{
					tiles[k] = new Tile();
					addChild(tiles[k]);
					tiles[k].init(levelXML.levels.level[currentLevel].tile[k].@type, levelXML.levels.level[currentLevel].tile[k].@x, levelXML.levels.level[currentLevel].tile[k].@y);
					tiles[k].addEventListener(MouseEvent.MOUSE_DOWN, tileDown);
				}
				addEventListener(MouseEvent.MOUSE_UP, tileUp);
				addEventListener(Event.ENTER_FRAME, update);
			} 	
			else 
			{
				//fix stack of objects so play again button and such are visible
				setChildIndex(hud, numChildren-1);
				gameOver = true;
				hud.tutorial_txt.text = "Game complete.";
				hud.playButton.visible = true;
				hud.playButton.addEventListener(MouseEvent.CLICK, nextGame);
			}
		}
		
		private function prevRoom():void 
		{
			if(currentLevel > 0)
				currentLevel--;
				
			if (currentLevel == 0)
			{
				for (var i:Number = 0; i < levelXML.levels[0].bg[currentLevel].tile.length(); i++)
				{
					background[i] = new Tile();
					addChild(background[i]);
					background[i].init(levelXML.levels.bg[currentLevel].tile[i].@type, levelXML.levels.bg[currentLevel].tile[i].@x, levelXML.levels.bg[currentLevel].tile[i].@y);
				}
				
				hud.tutorial_txt.text = "Click and drag the hero to a door to enter the next room.";
			}
			else if (currentLevel == 1)
			{
				hud.tutorial_txt.text = "Collect treasures for valuable bonus points.";
			}
			else if (currentLevel == 2)
			{
				hud.tutorial_txt.text = "Slide walls by clicking and dragging.";
			}
			else
			{
				hud.tutorial_txt.text = "";
			}

			if(currentLevel < (levelXML.levels[0].level as XMLList).length())
			{        
				if (currentLevel != 0)
				{
					for (var j:Number = tiles.length - 1; j >= 0; j--)
					{
						tiles[j].destroy();
						tiles.splice(j, 1);
					}
				}
			
				for (var k:Number = 0; k < levelXML.levels[0].level[currentLevel].tile.length(); k++)
				{
					tiles[k] = new Tile();
					addChild(tiles[k]);
					tiles[k].init(levelXML.levels.level[currentLevel].tile[k].@type, levelXML.levels.level[currentLevel].tile[k].@x, levelXML.levels.level[currentLevel].tile[k].@y);
					tiles[k].addEventListener(MouseEvent.MOUSE_DOWN, tileDown);
				}
				addEventListener(MouseEvent.MOUSE_UP, tileUp);
				addEventListener(Event.ENTER_FRAME, update);
			} 	
			else 
			{
				gameOver = true;
				hud.tutorial_txt.text = "Game complete.";
				hud.playButton.visible = true;
				hud.playButton.addEventListener(MouseEvent.CLICK, nextGame);
			}
		}
		
		public function nextGame(e:Event): void 
		{
			hud.playButton.visible = false;
			removeEventListener(MouseEvent.CLICK, nextGame);
			gameOver = false;
			currentLevel = -1;
			hud.resetPoints();
			hud.message_txt.text = "";
			
			for (var i:Number = background.length - 1; i >= 0; i--)
			{
				background[i].destroy();
				background.splice(i, 1);
			}
			
			for (var j:Number = tiles.length - 1; j >= 0; j--)
			{
				tiles[j].destroy();
				tiles.splice(j, 1);
			}
			nextRoom();
		}
	}
}